public class test {


        // 递归实现
        public static int recursiveSearch(int[] arr, int target, int left, int right) {
            if (left > right) return -1;
            int mid = left + (right - left) / 2;
            if (arr[mid] == target) {
                return mid;
            } else if (arr[mid] > target) {
                return recursiveSearch(arr, target, left, mid - 1);
            } else {
                return recursiveSearch(arr, target, mid + 1, right);
            }
        }

        // 迭代实现
        public static int iterativeSearch(int[] arr, int target) {
            int left = 0;
            int right = arr.length - 1;
            while (left <= right) {
                int mid = left + (right - left) / 2;
                if (arr[mid] == target) {
                    return mid;
                } else if (arr[mid] > target) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }
            return -1;
        }

        public static void main(String[] args) {
            int[] arr = {1, 3, 5, 7, 9, 11, 13};
            int target = 7;

            // 递归方式调用
            int recursiveResult = recursiveSearch(arr, target, 0, arr.length - 1);
            System.out.println("递归查找结果: " + (recursiveResult != -1 ? "找到，索引为 " + recursiveResult : "未找到"));

            // 迭代方式调用
            int iterativeResult = iterativeSearch(arr, target);
            System.out.println("迭代查找结果: " + (iterativeResult != -1 ? "找到，索引为 " + iterativeResult : "未找到"));
        }
    }

